本文已制作成完整电子书发布,这里只是简单介绍python3的常用操作,更多详情请查看《我的开发之路系列 - node.js学习指南》
前言
本书为本人在学习node.js过程中,从大量相关书籍和资料中汇总出的node.js开发精髓,其中包含大量实例,方便查阅,特在此分享,希望对大家有用。
其实在写本书的时候,我很纠结,到底要不要涉及js的基础知识。说实话,写基础知识是一件很无聊的事,而且理论上达到学习node阶段的同学应该是js还不错的同学,不过经过一番取舍,还是决定写一些ECMAScript的常用标准语法(注意JavaScript与ECMAScript的关系,ECMAScript是JavaScript的标准规范,JavaScript是ECMAScript的具体实现),便于查阅,顺便也复习一下js的知识。
我本人也是一个 node.js 的学习者, 不敢说自己有多高的水平, 只是在学习过程中做了详细的笔记而已。 在写作过程中难免有不尽人意的地方, 希望大家在阅读中提出勘误, 本人会极力纠正,谢谢。
node.js简介
Node.js 就是运行在服务端的 JavaScript,它是一个基于 Chrome V8 引擎的 JavaScript 运行时。 Node.js 使用高效、轻量级的事件驱动、非阻塞 I/O 模型。它的包生态系统,npm,是目前世界上最大的开源库生态系统。
CommonJS
在CommonJS这个规范下,每个.js文件都是一个模块,它们内部各自使用的变量名和函数名都互不冲突,例如,hello.js和main.js都申明了全局变量var s = 'xxx',但互不影响。
一个模块想要对外暴露变量(函数也是变量),可以用module.exports = variable;,一个模块要引用其他模块暴露的变量,用var ref = require('module_name');就拿到了引用模块的变量。
要在模块中对外输出变量,用:
1
| module.exports = variable;
|
输出的变量可以是任意对象、函数、数组等等。
要引入其他模块输出的对象,用:
1
| var foo = require('other_module');
|
引入的对象具体是什么,取决于引入模块输出的对象。
module.exports 和 exports
在Node环境中,有两种方法可以在一个模块中输出变量:
方法一:对module.exports赋值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function hello() { console.log('Hello, world!'); } function greet(name) { console.log('Hello, ' + name + '!'); } module.exports = { hello: hello, greet: greet };
|
方法二:直接使用exports:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| function hello() { console.log('Hello, world!'); } function greet(name) { console.log('Hello, ' + name + '!'); } function hello() { console.log('Hello, world!'); } exports.hello = hello; exports.greet = greet;
|
默认情况下,Node准备的exports变量和module.exports变量实际上是同一个变量,并且初始化为空对象{},于是,我们可以写:
1 2
| exports.foo = function () { return 'foo'; }; exports.bar = function () { return 'bar'; };
|
也可以写:
1 2
| module.exports.foo = function () { return 'foo'; }; module.exports.bar = function () { return 'bar'; };
|
换句话说,Node默认给你准备了一个空对象{},就可以直接往里面加东西。
但是,如果我们要输出的是一个函数或数组,那么,只能给module.exports赋值:
1
| module.exports = function () { return 'foo'; };
|
给exports赋值是无效的,因为赋值后,module.exports仍然是空对象{}。
基本模块
global
JavaScript有且仅有一个全局对象,在浏览器中,叫window对象。而在Node.js环境中,也有唯一的全局对象,但不叫window,而叫global,这个对象的属性和方法也和浏览器环境的window不同。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| > global.console Console { log: [Function: bound consoleCall], info: [Function: bound consoleCall], warn: [Function: bound consoleCall], error: [Function: bound consoleCall], dir: [Function: bound consoleCall], time: [Function: bound consoleCall], timeEnd: [Function: bound consoleCall], trace: [Function: bound consoleCall], assert: [Function: bound consoleCall], Console: [Function: Console], debug: [Function: debug], dirxml: [Function: dirxml], table: [Function: table], group: [Function: group], groupCollapsed: [Function: groupCollapsed], groupEnd: [Function: groupEnd], clear: [Function: clear], count: [Function: count], markTimeline: [Function: markTimeline], profile: [Function: profile], profileEnd: [Function: profileEnd], timeline: [Function: timeline], timelineEnd: [Function: timelineEnd], timeStamp: [Function: timeStamp] }
|
process
process 代表当前Node.js进程。
1 2 3 4 5
| process.version # 当前Node版本 process.platform # 系统[平台 process.arch # 系统架构 process.cwd() # 当前目录 process.chdir('/tmp') # 切换目录
|
process.nextTick()将在下一轮事件循环中调用
1 2 3 4
| process.nextTick(function () { console.log('nextTick callback!'); }); console.log('nextTick was set!');
|
1 2
| nextTick was set! nextTick callback!
|
程序即将退出时的回调函数
1 2 3
| process.on('exit', function (code) { console.log('about to exit with code: ' + code); });
|
平台判断
1 2 3 4 5
| if (typeof(window) === 'undefined') { console.log('node.js'); } else { console.log('browser'); }
|
读写文件
异步读文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| 'use strict'; var fs = require('fs'); fs.readFile('file1.txt', 'utf-8', function (err, data) { if (err) { console.log(err); } else { console.log(data); } }); console.log('after')
|
fs.readFile将读取一个文件,第二参数设置文件编码,第三参数为回调函数,接收一个err和data,当正常读取时,err参数为null,data参数为读取到的String。当读取发生错误时,err参数代表一个错误对象,data为undefined。
由于是异步IO,将先打印after,等待读取文件完毕,再打印出文件内容。
二进制文件的读取
1 2 3 4 5 6 7 8 9 10 11 12
| 'use strict'; var fs = require('fs'); fs.readFile('100.jpg', function (err, data) { if (err) { console.log(err); } else { console.log(data); console.log(data.length + ' bytes'); } });
|
当读取二进制文件时,不传入文件编码时,回调函数的data参数将返回一个Buffer对象。在Node.js中,Buffer对象就是一个包含零个或任意个字节的数组(注意和Array不同)。
Buffer对象可以和String作转换,例如,把一个Buffer对象转换成String:
1 2 3
| var text = data.toString('utf-8'); console.log(text);
|
或者把一个String转换成Buffer:
1 2 3
| var buf = Buffer.from(text, 'utf-8'); console.log(buf);
|
同步读文件
除了标准的异步读取模式外,fs也提供相应的同步读取函数。同步读取的函数和异步函数相比,多了一个Sync后缀,并且不接收回调函数,函数直接返回结果。
用fs模块同步读取一个文本文件的代码如下:
1 2 3 4 5 6 7 8 9 10 11
| 'use strict'; var fs = require('fs'); try { var data = fs.readFileSync('100.jpg', 'utf-8'); console.log(Buffer.from(data, 'utf-8')); } catch (err) { console.log(err) } console.log('after')
|
原异步调用的回调函数的data被函数直接返回,函数名需要改为readFileSync,其它参数不变。
如果同步读取文件发生错误,则需要用try...catch捕获该错误。
写文件
将数据写入文件是通过fs.writeFile()实现的:
1 2 3 4 5 6 7 8 9 10 11 12
| 'use strict'; var fs = require('fs'); var data = 'Hello, Node.js'; fs.writeFile('output.txt', data, function (err) { if (err) { console.log(err); } else { console.log('ok.'); } });
|
writeFile()的参数依次为文件名、数据和回调函数。如果传入的数据是String,默认按UTF-8编码写入文本文件,如果传入的参数是Buffer,则写入的是二进制文件。回调函数由于只关心成功与否,因此只需要一个err参数。
和readFile()类似,writeFile()也有一个同步方法,叫writeFileSync():
1 2 3 4 5 6
| 'use strict'; var fs = require('fs'); var data = 'Hello, Node.js'; fs.writeFileSync('output.txt', data);
|
HTTP服务器
request对象封装了HTTP请求,我们调用request对象的属性和方法就可以拿到所有HTTP请求的信息;
response对象封装了HTTP响应,我们操作response对象的方法,就可以把HTTP响应返回给浏览器。
用Node.js实现一个HTTP服务器程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 'use strict'; var http = require('http'); var server = http.createServer(function (request, response) { console.log(request.method + ': ' + request.url); response.writeHead(200, {'Content-Type': 'text/html'}); response.end('<h1>Hello world!</h1>'); }); server.listen(2000); console.log('Server is running at http://127.0.0.1:2000/');
|
在命令行运行该程序并打开浏览器访问http://127.0.0.1:2000/,将在浏览器输出Hello world!
文件服务器
url解析
1 2 3 4 5
| 'use strict'; var url = require('url'); console.log(url.parse('http://user:pass@host.com:8080/path/to/file?query=string#hash'));
|
输出
1 2 3 4 5 6 7 8 9 10 11 12 13
| Url { protocol: 'http:', slashes: true, auth: 'user:pass', host: 'host.com:8080', port: '8080', hostname: 'host.com', hash: '#hash', search: '?query=string', query: 'query=string', pathname: '/path/to/file', path: '/path/to/file?query=string', href: 'http://user:pass@host.com:8080/path/to/file?query=string#hash' }
|
path解析
使用path模块可以正确处理操作系统相关的文件路径。在Windows系统下,返回的路径类似于C:\Users\michael\static\index.html。
1 2 3 4 5 6 7 8 9
| 'use strict'; var path = require('path'); var workDir = path.resolve('.'); var filePath = path.join(workDir, 'pub', 'index.html');
|
实现文件服务器
file_server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| 'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require('http'); var root = path.resolve(process.argv[2] || '.'); console.log('Static root dir: ' + root); var server = http.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; console.log('pathname: ', pathname) var filepath = path.join(root, pathname); console.log('filepath: ', filepath) fs.stat(filepath, function (err, stats) { if (!err && stats.isFile()) { console.log('200 ' + request.url); response.writeHead(200); fs.createReadStream(filepath).pipe(response); } else { console.log('404 ' + request.url); response.writeHead(404); response.end('404 Not Found'); } }); }); server.listen(2000); console.log('Server is running at http://127.0.0.1:2000/');
|
没有必要手动读取文件内容。由于response对象本身是一个Writable Stream,直接用pipe()方法就实现了自动读取文件内容并输出到HTTP响应。
在命令行直接运行 nodefile_server,将当前工作目录作为文件服务器,如果当前目录下有一个叫index.html的文件,在浏览器中输入http://localhost:2000/index.html即可访问。
在命令行运行node file_server.js /path/to/dir,把/path/to/dir改成你本地的一个有效的目录,然后在浏览器中输入http://localhost:2000/index.html。
在浏览器输入http://localhost:8080/时,会返回404,原因是程序识别出HTTP请求的不是文件,而是目录。
参考资料
廖雪峰官方网站: node.js
粉丝日志: Nodejs学习路线图
粉丝日志: 从零开始nodejs系列文章